clear
use analysis

g imyear = year(forstdato)
g immonth = month(forstdato)

foreach x of  varlist intro20*{
    replace `x'=0 if `x'==.
}

g cntrybirth = real(fodeland)
tab kjoenn, g(kjoenn)
rename kjoenn1 male
g age_arrival = imyear-foedselsaar
g age_arrival_sq = age_arrival^2

g ever_intro = 0 if yrs_intro == 0
replace ever_intro = 1 if yrs_intro>0

foreach t of numlist 2000/2019{
g knr`t' = substr(grkrets`t', 1, 4)
}

g ba2000 = 49090
g ba2001 = 51360
g ba2002 = 54170
g ba2003 = 56861
g ba2004 = 58778
g ba2005 = 60699
g ba2006 = 62892
g ba2007 = 66812
g ba2008 = 70256
g ba2009 = 72881
g ba2010 = 75641
g ba2011 = 79216
g ba2012 = 82122
g ba2013 = 85245
g ba2014 = 88370
g ba2015 = 90068
g ba2016 = 92578
g ba2017 = 93634
g ba2018 = 96883
g ba2019 = 99858

foreach t of numlist 2000/2018{
	g emp`t' = 1 if wlonn`t'>ba`t'
	replace emp`t' = 0 if emp`t'==.
	g empf`t' = 1 if wlonn`t'>(2*ba`t')
	replace empf`t' = 0 if empf`t'==.
}

g i2000 = .70
g i2001 = .72
g i2002 = .73
g i2003 = .74
g i2004 = .75
g i2005 = .76
g i2006 = .78
g i2007 = .78
g i2008 = .81
g i2009 = .83
g i2010 = .85
g i2011 = .86
g i2012 = .87
g i2013 = .88
g i2014 = .90
g i2015 = .92
g i2016 = .96
g i2017 = .97
g i2018 = 1

foreach t of numlist 2000/2018{
	replace wlonn`t' = wlonn`t'*i`t'
	replace sosial`t' = sosial`t'*i`t'
}

 
foreach y of numlist 3/14{
replace regyear = imyear + `y'

g regemp`y' = .
g regempf`y' = .
g regwage`y' = .
g regsos`y' = .
g regkomnr`y' = "."
foreach t of numlist 2000/2018{
	replace regemp`y'  = emp`t' if regyear==`t'
	replace regempf`y'  = empf`t' if regyear==`t'
	replace regwage`y' = wlonn`t' if regyear==`t'
	replace regsos`y' = sosial`t' if regyear==`t'
}
foreach t of numlist 2000/2018{
	replace regkomnr`y' = knr`t' if regyear==`t'
}

replace regwage`y' = 0 if regwage`y'<0
} 

replace regyear = imyear + 15
g regkomnr15 = "."
foreach t of numlist 2000/2019{
	replace regkomnr15 = knr`t' if regyear==`t'
}



g avgemp = (regemp4 + regemp5 + regemp6 + regemp7 + regemp8 + regemp9 + regemp10)/7
g avgwage = (regwage4+ regwage5+ regwage6+ regwage7+ regwage8+ regwage9+ regwage10)/7
g avgsos = (regsos4+ regsos5+ regsos6+ regsos7+ regsos8+ regsos9+ regsos10)/7

g avgemplong = (regemp4 + regemp5 + regemp6 + regemp7 + regemp8 + regemp9 + regemp10 + regemp11 + regemp12 + regemp13 + regemp14)/11 
g avgwagelong = (regwage4+ regwage5+ regwage6+ regwage7+ regwage8+ regwage9+ regwage10+ regwage11+ regwage12+ regwage13+ regwage14)/11
g avgsoslong = (regsos4+ regsos5+ regsos6+ regsos7+ regsos8+ regsos9+ regsos10+ regsos11+ regsos12+ regsos13+ regsos14)/11

g cumemp4 = regemp4
g cumemp5 = regemp4 + regemp 
g cumemp6 = regemp4 + regemp + regemp6
g cumemp7 = regemp4 + regemp + regemp6+ regemp7
g cumemp8 = regemp4 + regemp + regemp6+ regemp7+ regemp8
g cumemp9 = regemp4 + regemp + regemp6+ regemp7+ regemp8 + regemp9
g cumemp10= regemp4 + regemp + regemp6+ regemp7+ regemp8 + regemp9+ regemp10
g cumemp11= regemp4 + regemp + regemp6+ regemp7+ regemp8 + regemp9+ regemp10+ regemp11
g cumemp12= regemp4 + regemp + regemp6+ regemp7+ regemp8 + regemp9+ regemp10+ regemp11+ regemp12
g cumemp13= regemp4 + regemp + regemp6+ regemp7+ regemp8 + regemp9+ regemp10+ regemp11+ regemp12+ regemp13
g cumemp14= regemp4 + regemp + regemp6+ regemp7+ regemp8 + regemp9+ regemp10+ regemp11+ regemp12+ regemp13 + regemp14

g cumwage4 = regwage4
g cumwage5 = regwage4 + regwage 
g cumwage6 = regwage4 + regwage + regwage6
g cumwage7 = regwage4 + regwage + regwage6+ regwage7
g cumwage8 = regwage4 + regwage + regwage6+ regwage7+ regwage8
g cumwage9 = regwage4 + regwage + regwage6+ regwage7+ regwage8 + regwage9
g cumwage10= regwage4 + regwage + regwage6+ regwage7+ regwage8 + regwage9+ regwage10
g cumwage11= regwage4 + regwage + regwage6+ regwage7+ regwage8 + regwage9+ regwage10+ regwage11
g cumwage12= regwage4 + regwage + regwage6+ regwage7+ regwage8 + regwage9+ regwage10+ regwage11+ regwage12
g cumwage13= regwage4 + regwage + regwage6+ regwage7+ regwage8 + regwage9+ regwage10+ regwage11+ regwage12+ regwage13
g cumwage14= regwage4 + regwage + regwage6+ regwage7+ regwage8 + regwage9+ regwage10+ regwage11+ regwage12+ regwage13 + regwage14

g cumsos4 = regsos4
g cumsos5 = regsos4 + regsos 
g cumsos6 = regsos4 + regsos + regsos6
g cumsos7 = regsos4 + regsos + regsos6+ regsos7
g cumsos8 = regsos4 + regsos + regsos6+ regsos7+ regsos8
g cumsos9 = regsos4 + regsos + regsos6+ regsos7+ regsos8 + regsos9
g cumsos10= regsos4 + regsos + regsos6+ regsos7+ regsos8 + regsos9+ regsos10
g cumsos11= regsos4 + regsos + regsos6+ regsos7+ regsos8 + regsos9+ regsos10+ regsos11
g cumsos12= regsos4 + regsos + regsos6+ regsos7+ regsos8 + regsos9+ regsos10+ regsos11+ regsos12
g cumsos13= regsos4 + regsos + regsos6+ regsos7+ regsos8 + regsos9+ regsos10+ regsos11+ regsos12+ regsos13
g cumsos14= regsos4 + regsos + regsos6+ regsos7+ regsos8 + regsos9+ regsos10+ regsos11+ regsos12+ regsos13 + regsos14

foreach t of numlist 4/14{
	replace union`t' = . if regwage`t'==0
	replace union`t' = . if regwage`t'==.
}

egen everunion = rowmax(union4 union5 union6 union7 union8 union9 union10 union11 union12 union13 union14)
replace everunion = 0 if everunion==. & avgemp!=.

egen eunion4 = rowmax(union4)
egen eunion5 = rowmax(union4 union5)
egen eunion6 = rowmax(union4 union5 union6)
egen eunion7 = rowmax(union4 union5 union6 union7)
egen eunion8 = rowmax(union4 union5 union6 union7 union8)
egen eunion9 = rowmax(union4 union5 union6 union7 union8 union9)
egen eunion10 = rowmax(union4 union5 union6 union7 union8 union9 union10)
egen eunion11 = rowmax(union4 union5 union6 union7 union8 union9 union10 union11)
egen eunion12 = rowmax(union4 union5 union6 union7 union8 union9 union10 union11 union12)
egen eunion13 = rowmax(union4 union5 union6 union7 union8 union9 union10 union11 union12 union13)
egen eunion14 = rowmax(union4 union5 union6 union7 union8 union9 union10 union11 union12 union13 union14)
foreach x in eunion4 eunion5 eunion6 eunion7 eunion8 eunion9 eunion10 eunion11 eunion12 eunion13 eunion14{
    replace `x' = 0 if `x'==. & avgemp!=.
}

 g cityr  = year(date_citizen)
g citdays = date_citizen - forstdato
g norcitizen = 0
replace norcitizen = 1 if norcitizenship == 0 & citdays<=(365*10) & age_arrival>17
replace norcitizen = 1 if norcitizenship == 0 & citdays<=(365*7) & age_arrival<18
replace norcitizen = . if doedsaar<(imyear+9)

g norcitizen14 = 0
replace norcitizen14 = 1 if norcitizenship == 0 & citdays<=(365*14) 
replace norcitizen14 = . if doedsaar<(imyear+9)

replace regyear = imyear + 1

g knr_arrival="."
foreach t of numlist 2000/2019{
replace knr_arrival = knr`t' if regyear==`t'
}

replace regyear = imyear + 1
g civstat_arrival = "."
foreach t of numlist 2001/2018{
	replace civstat_arrival  = civstatus`t' if regyear==`t'
}
g married_arrival = 0
replace married_arrival = 1 if civstat_arrival == "2"
replace married_arrival = 1 if civstat_arrival == "6"

g famtype_arrival = "."
replace famtype_arrival = famtype2005 if imyear<=2004
foreach t of numlist 2005/2018{
	replace famtype_arrival  = famtype`t' if regyear==`t'
}
g children_arrival = 0
foreach t of numlist 2 3 4 5 6{
replace children_arrival = 1 if famtype_arrival=="`t'"
}


g edu_arrival = "."
foreach t of numlist 2000/2018{
	replace edu_arrival  = bu`t' if regyear==`t'
}
g redu_arrival = substr(edu_arrival, 1, 1)
g redu_arrival2 = real(redu_arrival)
recode redu_arrival2 (0 1 2=1)(else=0),g(lowedu_arrival)
recode redu_arrival2 (6 7 8=1)(else=0),g(highedu_arrival)
recode redu_arrival2 (.=1)(else=0),g(missedu_arrival)
g educat_arrival = redu_arrival
drop redu_arrival  


g norw_arrival = .
foreach t of numlist 2000/2018{
	replace norw_arrival  = norw`t' if regyear==`t'
}

replace regyear = imyear + 10
g edu10 = "."
foreach t of numlist 2005/2018{
	replace edu10  = bu`t' if regyear==`t'
}
g redu10 = substr(edu10, 1, 1)
g redu10_2 = real(redu10)
recode redu10_2 (0 1 2=1)(else=0),g(lowedu_yr10)
recode redu10_2 (6 7 8=1)(else=0),g(highedu_yr10)
recode redu10_2 (0 1 2=0)(3 4 5 = 1)(6 7 8=2)(else=0),g(educat10)
drop redu10  
g d_highedu = highedu_yr10 - highedu_arrival
replace d_highedu = 1 if d_highedu==2

foreach t of numlist 1/15{
	g norw_y`t' = .
}

foreach p of numlist 1/15{
replace regyear = imyear + `p'
foreach t of numlist 2004/2019{
	replace norw_y`p'  = norw`t' if regyear==`t'
}
}

g moved = .
replace moved = 1 if knr_arrival!=regkomnr15
replace moved = 0 if knr_arrival==regkomnr15
replace moved = . if knr_arrival==""
replace moved = . if regkomnr10==""
  
foreach t of numlist 4/14{
	g regpartnercntry`t' = "."
	g intramarr`t' = 0
	g union`t' = 0
}

foreach p of numlist 4/14{
replace regyear = imyear + `p'
foreach t of numlist 2005/2018{
	replace regpartnercntry`p'  = landbak_partner`t' if regyear==`t'
	replace intramarr`p' = 1 if landbak3gen == regpartnercntry`p'
}
}

foreach p of numlist 4/14{
replace regyear = imyear + `p'
foreach t of numlist 2004/2018{
	replace union`p' = union`t' if regyear==`t' & union`t'!=.
}
}

 
g afghanistan = 0
replace afghanistan = 1 if landbak3gen=="404"
g iraq = 0
replace iraq = 1 if landbak3gen=="452"
g iran = 0
replace iran = 1 if landbak3gen=="456"

g continent = substr( landbak3gen, 1, 1)
g africa = 0
replace africa = 1 if continent == "2"
replace africa = 1 if continent == "3"
g asia = 0
replace asia = 1 if continent == "4"
replace asia = 1 if continent == "5"

g norwegian = 0
replace norwegian = 1 if norcitizenship==0
 
 
 
g county_arrival = substr(knr_arrival, 1, 2)
destring county_arrival, replace
recode county_arrival (2 3 = 1 "Oslo/Akershus")(4 5 = 2 "Hedmark/Oppland")(1 6 7 8 = 3 "South-East Norway")(9 10 11 = 4 "Agder/Rogaland")(12 14 15 = 5 "West Norway") (16 17 = 6 "Trondelag")(18 19 20 = 7 "Northern Norway")(else = .), g(region_arrival)
qui tab region_arrival, g(region_arrival_)

foreach x in intramarr4 intramarr5 intramarr6 intramarr7 intramarr8 intramarr9 intramarr10 intramarr11 intramarr12 intramarr13 intramarr14{
    recode `x' (1=0)(0=1)
}

foreach t of numlist 4/14{
	replace intramarr`t' = 0 if regpartnercntry`t'==""
}

g grkrets_arrival = "."
replace grkrets_arrival = grkrets2003 if imyear==2002
replace grkrets_arrival = grkrets2004 if imyear==2003
replace grkrets_arrival = grkrets2005 if imyear==2004

merge 1:1 lopenr using yrfirstbrn
drop if _merge==2
drop _merge

replace children_arrival = 1 if children_arrival == 0 & yrfirstbrn<imyear
replace children_arrival = 0 if children_arrival == 1 & yrfirstbrn!=. & yrfirstbrn>imyear

bys cntry: g cntryobs = _N
g smallcntry = 0 
replace smallcntry = 1 if cntryobs<100
g cntry_o = cntry
replace cntry_o = . if smallcntry==1
replace cntry_o = 999 if smallcntry==1
replace cntry = cntry_o


drop if knr_arrival=="" 
egen munic_arrival = group(knr_arrival)
egen cntry = group(landbak3gen)

qui tab igrus3, g(igrus3_)
rename igrus3_1 unref
rename igrus3_2 asylref

g outmig = 0 if tilland_aar==.
replace outmig = 1 if tilland_aar!=.

drop if imyear<1998
drop if imyear>2007 


g eligage = 0
replace eligage = 1 if age_arrival>16 & age_arrival<56
keep if eligage == 1

keep if igrus3=="711" | igrus3=="712"  
drop if inngrunn1=="ANN"
drop if inngrunn1=="ARB"
drop if inngrunn1=="UTD"
drop if famforentype!=""
drop if mor_lnr!=""
drop if far_lnr!=""

save usingfile, replace